#include <xpp/xli/XLIReader.h>
#include <xpp/xli/XLIWriter.h>
#include <xp/bas/stdio.h>
#include <xp/bas/sio.h>

#ifdef __linux
#include <mcheck.h>
#endif

using namespace xpp::xli;

class MyXLIReader: public XLIReader
{
public:
	int readChar (xp_char_t* c)
	{
		xp_ssize_t n;
		xp_char_t tmp;

		n = xp_sio_getc (xp_sio_in, &tmp);
		if (n == -1) return -1;
		if (n == 0) {
			*c = XP_CHAR_EOF;
			return 0;
		}

		*c = tmp;
		return 1;
	}
};

class MyXLIWriter: public XLIWriter
{
public:
	int writeChar (xp_char_t c)
	{
		xp_ssize_t n;
		n = xp_sio_putc (xp_sio_out, c);
		if (n < 0) return -1;
		if (n > 0) return 1;
		return n;
	}
};

void read_xli ()
{
	MyXLIReader xr;
	MyXLIWriter xw;

	XLI* xli = xr.read ();
	XLI* xli2;
	if (xli) {
		xli2 = (XLI*)xli->copy ();
		delete xli;

		xw.write (xli2);	
		delete xli2;
	}
	else {
		xp_printf (
			XP_TEXT("line %u, col %u - %s\n"), 
			(unsigned)xr.errorLine(), 
			(unsigned)xr.errorCol(), xr.errorStr());
	}
}


int main ()
{
#ifdef __linux
	mtrace ();
#endif

	read_xli ();

#ifdef __linux
	muntrace ();
#endif
	return 0;
}



